package com.ruoyi.quartz.task;

import com.ruoyi.system.domain.SysStudent;
import com.ruoyi.system.service.ISysStudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * 学生信息模拟创建定时任务
 * 
 * @author ruoyi
 */
@Component("studentTask")
public class StudentTask
{
    private static final Logger log = LoggerFactory.getLogger(StudentTask.class);

    @Autowired
    private ISysStudentService studentService;
    
    /**
     * 模拟创建一条学生数据
     */
    public void createStudent()
    {
        log.info("开始执行学生信息模拟创建任务");
        
        try {
            // 创建学生对象
            SysStudent student = new SysStudent();
            
            // 生成随机学生信息
            student.setStudentName(generateRandomName());
            student.setSex(generateRandomSex());
            student.setIntroduction(generateRandomIntroduction());
            student.setAvatar("default-avatar.jpg"); // 使用默认头像
            student.setStatus("0"); // 状态设置为正常
            student.setDelFlag("0"); // 未删除
            
            // 插入学生数据
            int result = studentService.insertSysStudent(student);
            
            if (result > 0) {
                log.info("学生信息创建成功：{}", student.getStudentName());
            } else {
                log.error("学生信息创建失败");
            }
        } catch (Exception e) {
            log.error("执行学生信息模拟创建任务时发生异常", e);
        }
        
        log.info("学生信息模拟创建任务执行完毕");
    }
    
    /**
     * 生成随机姓名
     */
    private String generateRandomName() {
        String[] surnames = {"张", "王", "李", "赵", "刘", "陈", "杨", "黄", "周", "吴"};
        String[] names = {"明", "华", "强", "伟", "芳", "娜", "秀英", "敏", "静", "丽", "强", "磊", "军", "洋", "勇"};
        
        Random random = new Random();
        String surname = surnames[random.nextInt(surnames.length)];
        String name = names[random.nextInt(names.length)];
        
        // 80%的概率生成双字名
        if (random.nextDouble() < 0.8) {
            name += names[random.nextInt(names.length)];
        }
        
        return surname + name;
    }
    
    /**
     * 生成随机性别
     */
    private String generateRandomSex() {
        return new Random().nextBoolean() ? "0" : "1"; // 0男 1女
    }
    
    /**
     * 生成随机个人介绍
     */
    private String generateRandomIntroduction() {
        String[] introductions = {
            "我是一名热爱学习的学生。",
            "喜欢编程和阅读。",
            "积极向上，乐于助人。",
            "热爱运动，特别是篮球。",
            "对新技术充满好奇。",
            "努力学习，追求进步。",
            "喜欢结交新朋友。",
            "梦想成为一名优秀的工程师。",
            "热爱生活，热爱学习。",
            "性格开朗，乐观向上。"
        };
        
        return introductions[new Random().nextInt(introductions.length)];
    }
}